/**
 * Created on 20/04/2008
 * 
 */
package com.apbetioli.mapr.place.entity;

import com.apbetioli.mapr.place.schema.CtGeoPoint;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * Persistent entity class for coordinates. 
 * 
 * Decided to make this entity embeddable because i don't need to create a 
 * separated primary key. And simplifies deleting, since one line per place 
 * represents all the information. The other purpose is that no address will 
 * be duplicated, neither coordinates, because there won't be 2 markers in 
 * the same place. Simplifying, this is the easyest way to make it run.
 *
 * The columns are not set with "nullable = false" because this validation 
 * will happen in the xml parsing.
 * 
 * @author Alexandre Parra Betioli
 */
@Embeddable
public class GeoPoint extends CtGeoPoint implements Serializable {
    
    /*---------------------------------------------*/
    // Columns
    /*---------------------------------------------*/
    
    @Column( name="LATITUDE" )
    @Override
    public double getLatitude() {
        return super.getLatitude();
    }

    
    @Column( name="LONGITUDE" )
    @Override
    public double getLongitude() {
        return super.getLongitude();
    }
    
    /*---------------------------------------------*/
    // Methods
    /*---------------------------------------------*/
    
    /**
     * It was necessary because can't cast CtGeoPoint to GeoPoint
     */
    public void parse( final CtGeoPoint ctGeopoint ) {
        if( ctGeopoint == null)
            throw new IllegalArgumentException("GeoPoint is null.");
            
        this.setLatitude( ctGeopoint.getLatitude() );
        this.setLongitude( ctGeopoint.getLongitude() );
    }
    
    @Override
    public int hashCode() {
        String hash = "" + getLatitude() + getLongitude();
        return hash.hashCode();
    }

    @Override
    public boolean equals(Object object) {

        if( object == null )
            return false;
        
        if (!(object instanceof GeoPoint))
            return false;
        
        GeoPoint other = (GeoPoint) object;
        
        if ((this.getLatitude()  != other.getLatitude()  )  ||
            (this.getLongitude() != other.getLongitude() )) {
            return false;
        }
        
        return true;
    }
    
    @Override
    public String toString() {
        return "GeoPoint[latitude=" + getLatitude() + ", longitude=" + getLongitude() + "]";
    }

    @Override
    public GeoPoint clone() {
        GeoPoint g = new GeoPoint();
        g.setLatitude( this.getLatitude() );
        g.setLongitude( this.getLongitude() );
        return g;
    }
    
}
